<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../layui/layui.js" charset="utf-8"></script>
</head>

<body>
    <div class="layui-container">
        <div class="layui-row">
            <div class="layui-col-lg-offset3" layui-col-lg6>
                <div class="layui-form layuimini-form">
                    <form class="layui-form" action="" lay-filter="userForm">

                        <div class="layui-form-item">
                            <label class="layui-form-label">请选择导入文件：</label>
                            <input type="hidden" name="excel" id="excel">
                            <div class="layui-input-block">
                                <button type="button" class="layui-btn" id="durl">
                                    <i class="layui-icon">&#xe67c;</i>上传文件
                                </button>

                                <div class="layui-upload-list">
                                    <img style="width: 100px" class="layui-upload-img" id="demo1">
                                    <p id="demoText"></p>
                                </div>

                                <div style="width: 95px;">
                                    <div class="layui-progress layui-progress-big" lay-showpercent="yes"
                                        lay-filter="demo">
                                        <div class="layui-progress-bar" lay-percent=""></div>
                                    </div>
                                </div>

                            </div>

                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                                提示：请上传excel文件，文件名为“学生信息表.xlsx”
                            </div>
                        </div>


                    </form>
                </div>

            </div>
        </div>
    </div>



    <script>
        //Demo
        layui.use(['form', 'element', 'upload'], function () {
            var form = layui.form
                , $ = layui.jquery
                , element = layui.element
                , upload = layui.upload
                , laydate = layui.laydate;

            //日期   渲染上面 id 为 date 的 标签
            laydate.render({
                elem: '#date'
            });

            //执行实例
            var uploadInst = upload.render({
                elem: '#durl' //绑定元素
                , accept: 'file' //普通文件
                , exts: 'xlsx' //只允许上传压缩文件
                , url: 'http://localhost:8080/user/import' //上传接口

                , done: function (res) {
                    //上传完毕回调
                    if (res.code == 0) {

                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.msg("上传成功");
                        parent.layer.close(iframeIndex);

                    }
                }

                //进度条
                , progress: function (n, elem, e) {
                    element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                    if (n == 100) {
                        layer.msg('上传完毕', { icon: 1 });
                    }
                }
            });


            //监听提交
            form.on('submit(formDemo)', function (data) {
                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.msg("上传成功");
                parent.layer.close(iframeIndex);
                $.ajax({
                    "url": "http://localhost:8080/student/addStudent",
                    "data": data.field,
                    "success": function (result) {
                        if (result.code == 0) {

                            // 关闭 弹出层
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.msg("添加成功");
                            parent.layer.close(iframeIndex);

                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    "dataType": "json"
                });
                return false;
            });
        });
    </script>


</body>

</html>